<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item v-show="false"  label="类型"  :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'type', validatorRules.type]" placeholder="请输入编号"></a-input>
        </a-form-item>


        <a-form-item label="所属项目" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'projectCode', validatorRules.projectCode]" placeholder="请选择所属项目" @change="handleChangeProjectCode">
            <a-select-option v-for="item in projectList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="所属路灯灯杆" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select  v-decorator="[ 'lightPoleCode', validatorRules.lightPoleCode]" placeholder="请选择所属路灯灯杆">
            <a-select-option v-for="item in lightPoleList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item label="编号" v-if="model.id" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'code', validatorRules.code]" placeholder="请输入编号"></a-input>
        </a-form-item>

        <a-form-item label="网关编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'gateWayCode', validatorRules.gateWayCode]" placeholder="请输入网关编号"></a-input>
        </a-form-item>

        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'lightTypeCode', validatorRules.lightTypeCode]" placeholder="请选择类型">
            <a-select-option v-for="item in typeList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="竣工日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择竣工日期" v-decorator="[ 'finishDate', validatorRules.finishDate]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>



        <a-form-item label="通讯方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag   v-decorator="['socketType', validatorRules.socketType]" :triggerChange="true" placeholder="网关类型"
                               dictCode="	glr_socket_type"/>
        </a-form-item>

        <a-form-item label="经度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'longitude', validatorRules.longitude]" placeholder="请输入经度"></a-input>
        </a-form-item>
        <a-form-item label="纬度" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'latitude', validatorRules.latitude]" placeholder="请输入纬度"></a-input>
        </a-form-item>
        <a-form-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea  v-decorator="[ 'posDesc', validatorRules.posDesc]" placeholder="请输入描述"></a-textarea>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: "GlrAloneLightModel",
    components: {
      JDate,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        lightPoleList:[],
        projectList:[],
        typeList:[],
        gatewayList:[],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          code: {rules: [
            ]},
          lightTypeCode: {rules: [
              {required: true, message: '请选择路灯类型!'},
            ]},
          gateWayCode: {rules: [
                {required: true, message: '请选择网关编号'},
            ]},
          projectCode: {rules: [
              {required: true, message: '请选择项目!'},
            ]},
          socketType: {rules: [
              {required: true, message: '请选择通信方式!'},
            ]},
          finishDate: {rules: [
            ]},
          lightPoleCode: {rules: [
            ]},
          longitude: {rules: [
            ]},
          latitude: {rules: [
            ]},
          posDesc: {rules: [
            ]},
          currentVol: {rules: [
            ]},
          currentCur: {rules: [
            ]},
          currentBrightness: {rules: [
            ]},
          currentApparentPower: {rules: [
            ]},
          currentPowerFactor: {rules: [
            ]},
          currentPower: {rules: [
            ]},
          currentStatus: {rules: [
            ]},
          currentTemperature: {rules: [
            ]},
          sysCompanyCode: {rules: [
            ]},
        },
        url: {
          add: "/glr/glrLight/add",
          edit: "/glr/glrLight/edit",
          getLightType: "/glr/glrLightType/getLightType",
          getGateWay: "/glr/glrGateway/getGateWay",
          getLightPoleList: "/glr/glrLightPole/getLightPoleList",
          getProjectList: "/glr/glrProject/getProject",
        }
      }
    },
    created () {
    },
    methods: {
      add (gateWayCode) {
        this.gateWayCode=gateWayCode;
        this.edit({});
      },
      edit (record) {
        const that = this;
        httpAction(that.url.getProjectList,{},"post").then((res)=>{
          if(res.success){
            that.projectList=res.result;
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
        this.form.resetFields();
        this.model = Object.assign({}, record);
        if(this.model.id==null){
          this.model.socketType='NRIOT';
          this.model.type='2';
        }
        this.visible = true;
        httpAction(this.url.getLightType,null,"post").then((res)=>{
          if(res.success){
            that.typeList=res.result;
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'gateWayCode','type','socketType','projectCode','code','lightTypeCode','isWisdom','finishDate','longitude','latitude','posDesc','currentVol','currentCur','currentBrightness','currentApparentPower','currentPowerFactor','currentPower','currentStatus','currentTemperature','sysCompanyCode'))
        })

        if (this.model.projectCode){
          this.handleChangeProjectCode(this.model.projectCode);
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'code','lightTypeCode','isWisdom','gateWayCode','finishDate','longitude','latitude','posDesc','currentVol','currentCur','currentBrightness','currentApparentPower','currentPowerFactor','currentPower','currentStatus','currentTemperature','sysCompanyCode'))
      },
      handleChangeProjectCode(projectCode){
        const that = this;
        httpAction(that.url.getLightPoleList,{projectCode:projectCode},"post").then((res)=>{
          if(res.success){
            that.lightPoleList=res.result;
            if (that.lightPoleList.length>0){
              that.form.setFieldsValue({'lightPoleCode': that.lightPoleList[0].code});
            }
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      }

    }
  }
</script>